package com.pks.autoInsurance.helper;

import java.util.Map;

import com.pks.autoInsurance.dao.RegistrationDAO;
import com.pks.autoInsurance.dao.RegistrationDAOImpl;
import com.pks.autoInsurance.dto.LoginDto;
import com.pks.autoInsurance.dto.PersonalDetailsDto;

public class RegistrationHelper {
	/*
	 * Performs registration of the user
	 */
	public boolean registerUser(PersonalDetailsDto personalDetailsDtoReq,
			LoginDto loginDtoReq) {
		System.out.println(getClass() + "::::registerUser::::begin");
		RegistrationDAO registrationDAO = new RegistrationDAOImpl();
		System.out.println("::::personalDetailsDtoReq::::"
				+ personalDetailsDtoReq);
		System.out.println(getClass() + "::::registerUser::::end");
		return registrationDAO.createUser(personalDetailsDtoReq, loginDtoReq);
	}

	/*
	 * Checks for existing user presence in the system
	 */
	public boolean isExistingUser(LoginDto loginDtoReq) {
		System.out.println(getClass() + "::::isExistingUser::::begin");
		RegistrationDAO registrationDAO = new RegistrationDAOImpl();
		System.out.println("::::loginDtoReq::::" + loginDtoReq);
		System.out.println(getClass() + "::::isExistingUser::::end");
		return registrationDAO.isExistingUser(loginDtoReq);
	}

	/*
	 * Retrives premium quote for a user
	 */
	public Map<String, String> getQuote(String userId) {
		System.out.println(getClass() + "::::getQuote::::begin");
		RegistrationDAO registrationDAO = new RegistrationDAOImpl();
		Map<String, String> map = registrationDAO.getQuote(userId);
		int age = (!map.get("Age").equals("")) ? Integer.parseInt(map
				.get("Age")) : 0;
		int totalAccident = (!map.get("Total Accident").equals("")) ? Integer
				.parseInt(map.get("Total Accident")) : 0;
		int policyAmount = (!map.get("Policy Amount").equals("")) ? Integer
				.parseInt(map.get("Policy Amount")) : 1;
		float riskFactor = (float) age / 210 + (float) totalAccident / 10;
		float premiumAnnually = policyAmount * riskFactor;
		float premiumQuarterly = premiumAnnually / 4;
		float premiumMonthly = premiumAnnually / 12;
		map.put("premiumAnnually", String.valueOf(premiumAnnually));
		map.put("premiumQuarterly", String.valueOf(premiumQuarterly));
		map.put("premiumMonthly", String.valueOf(premiumMonthly));

		// To Debug map data
		// for (Iterator<Map.Entry<String, String>> iterator = map.entrySet()
		// .iterator(); iterator.hasNext();) {
		// Map.Entry<String, String> entry = iterator.next();
		// System.out.println(entry.getKey() + " " + entry.getValue());
		// }
		System.out.println(getClass() + "::::getQuote::::end");
		return map;
	}

}
